
function HttpSwitch(){
	
	 var activeX =['MSXML4.XMLHTTP','MSXML3.XMLHTTP',
	                   'MSXML2.XMLHTTP','Msxml.XmlHttp'];
	                   
	try {
		 this.xmlHttp = new XMLHttpRequest();
		 this.xmlHttp.Control = this;
	} catch(e) {
    	 if (window.ActiveXObject)
    	 {
	    	 for (var i = 0; i < activeX.length; i++) 
	         {
	             try {
	     	 
	            	 this.xmlHttp = new ActiveXObject(activeX[i]);
	           	
	                 break;
	            } catch(e) {}
	         }
    	 }
     } finally { }
	

     this.isSendSynch = false;	

     this.url         = window.contextPath +"/managerService";
  
     this.refresh     = window.contextPath +"/managerService"; 
     
     this.swType      = "POST";
     
     this.callBack    = null;	
	
    HttpSwitch.prototype.setUrl			    = HttpSwitch_setHttpUrl;

    HttpSwitch.prototype.setSendType		= HttpSwitch_setSendType;
   
    HttpSwitch.prototype.setSwitchType		= HttpSwitch_setSwitchType;
	
    HttpSwitch.prototype.setCallBack		= HttpSwitch_setCallBack;
	
    HttpSwitch.prototype.getText				= HttpSwitch_getText;	

    HttpSwitch.prototype.getXml				    = HttpSwitch_getXml;	

    HttpSwitch.prototype.getBody				= HttpSwitch_getBody;	

    HttpSwitch.prototype.sendAsynchronism      = HttpSwitch_sendAsynchronism;	

    HttpSwitch.prototype.sendSynchronization   = HttpSwitch_sendSynchronization;	

    HttpSwitch.prototype.send                  = HttpSwitch_send;

    HttpSwitch.prototype.free                  = HttpSwitch_free;
}

function HttpSwitch_setHttpUrl(UrlStr){
	if( UrlStr == "" || UrlStr == null){
		alert("服务器地址不能为空！");
		return;	
	}
	this.url = window.contextPath + UrlStr;
}

function HttpSwitch_setSendType(isSynch)
{
	if(isSynch == false)
	{
		this.isSendSynch = false;
	}else{
		this.isSendSynch = true;
	}
}
function HttpSwitch_setSwitchType(switchType)
{
	if(switchType && switchType.toLowerCase()== "get")
		this.swType ="GET";
	else
		this.swType ="POST";
}

function HttpSwitch_setCallBack(callBack){
	this.callBack = callBack;
}
function HttpSwitch_getText(){
	if (this.xmlHttp == null)
		return "";
	else
		return this.xmlHttp.responseText;
}
function HttpSwitch_getXml(){
	if (this.xmlHttp == null)
		return "";
	else	
		return this.xmlHttp.responseXML;
}
function HttpSwitch_getBody(){
	if (this.xmlHttp == null)
		return "";
	else	
		return this.xmlHttp.responseBody;
}
/**
 * 数据异步传输
 */

function HttpSwitch_sendAsynchronism(sendXML)
{
	if (this.refresh == ""){
		alert("服务器地址不能为空！");
		return;	
	}
	if (window.log.allControl&&window.log.refreshSend)
	{
		alert("HttpSwitch:sendAsynchronism:refreshSend:异步数据"+sendXML);
	}
	
	this.xmlHttp.open(this.swType, this.refresh, true);
	this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	if (sendXML)
		this.xmlHttp.send(sendXML);
	else
		this.xmlHttp.send();
	
	var parent = this;
	this.xmlHttp.onreadystatechange = function()
	{		
		if(parent.xmlHttp.readyState != 4 || parent.xmlHttp.status != 200)
			return;
		
		if (parent.compact)
		{
			if (parent.compact.load)
				parent.compact.load();	
		}else{
			switch (typeof(parent.callBack)){
				case "string":{
					yacon.fireUserEvent(parent.callBack,[parent])
					break;
				}
				case "object":{
					parent.callBack(parent);	
					break;
				}			
			}
		}	
	}
}
/**
 * 数据同步传输
 */
function HttpSwitch_sendSynchronization(sendXML)
{
	if (this.url == ""){
		alert("服务器地址不能为空！");
		return;	
	}	
	
	if (window.log.allControl&&window.log.excuteSend)
	{
		alert("HttpSwitch:sendSynchronization:excuteSend:同步数据"+sendXML);
	}
	this.xmlHttp.onreadystatechange= function(){};
	this.xmlHttp.open(this.swType, this.url, false);
	this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	

	if (sendXML)
		this.xmlHttp.send(sendXML);
	else
		this.xmlHttp.send();
	
	if ( this.xmlHttp.status == 200 ) 
	{
		if (this.compact)
		{
			if (this.compact.load)
				this.compact.load();	
		}else{
			switch (typeof(this.callBack)&&this.callBack!=null){
				case "string":{
					yacon.fireUserEvent(this.callBack,[this])
					break;
				}
				case "object":{
					this.callBack(this);	
					break;
				}
			}
		}
    }
}
function HttpSwitch_send(sendXML)
{
	if(this.isSendSynch)
	{
		this.sendSynchronization(sendXML);
	}else{
		this.sendAsynchronism(sendXML);
	}
}
function HttpSwitch_free(){	
	this.xmlHttp = null;	
	delete 	this.xmlHttp;
}	

